package com.cgy.utils.elasticsearch.client;

import com.cgy.utils.elasticsearch.ElasticHelper;
import com.cgy.utils.netx.client.INetworkClientLibrary;
import com.cgy.utils.netx.config.ClientConfig;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;

/**
 * @author CHTK
 */
@Slf4j
public class ElasticsearchClient implements INetworkClientLibrary<ElasticHelper> {

    private ElasticHelper elasticHelper;

    @Override
    public INetworkClientLibrary initial(ClientConfig clientConfig, Object... extConfig) {
        Preconditions.checkArgument(null != clientConfig);
        Preconditions.checkArgument(StringUtils.isNotBlank(clientConfig.getIp()));
        this.elasticHelper = new ElasticHelper(clientConfig);
        return this;
    }

    @Override
    public INetworkClientLibrary initial(InputStream inputStream) {
        return this;
    }

    @Override
    public INetworkClientLibrary connect() throws Exception {
        elasticHelper.configure();
        log.info("elasticsearch客户端连接成功.....");
        return this;
    }

    @Override
    public void release() throws Exception {
        this.elasticHelper.close();
    }

    @Override
    public ElasticHelper getClient() throws Exception {
        return elasticHelper;
    }
}
